{include file="admin@public/header" /}

<link rel="stylesheet" href="/public/static/admin/easyui/layui-v2.6.8/css/layui.css">
<link href="/public/static/admin/blue/bills.css" rel="stylesheet" type="text/css">

<style>
    .mod-search {
        position: relative;
    }

    #custom {
        position: absolute;
        top: 0;
        right: 0;
    }

    .ui-jqgrid-bdiv .ui-state-highlight {
        background: none;
    }

    #manager li {
        margin: 8px 0;
    }

    #manager .ui-label {
        width: 204px;
        display: inline-block;
        line-height: 18px;
        font-size: 14px;
        text-align: center;
    }

    #manager .ui-label-warning:hover {
        background-color: #FFBA5A;
    }

    .no-query {
        border: none;
    }

    .bill-ser-top {
        font-size: 12px;
    }

    .layui-form-checkbox[lay-skin=primary] i {
        padding: 0;
    }

    .layui-form-checked[lay-skin=primary] i {
        border-color: #5dc5e7;
        background-color: #5dc5e7;
        color: #fff;
        padding: 0;
    }

    .layui-form-checkbox[lay-skin=primary]:hover i {
        border-color: #5dc5e7;
    }

    .ui-droplist-wrap .list-item:hover {
        background-color: #faffad;
    }

    .shadow.opera-log-box::before,
    .shadow.opera-log-box::after {
        border: solid transparent;
        content: ' ';
        height: 0;
        bottom: 100%;
        position: absolute;
        width: 0;
    }

    .shadow.opera-log-box:after {
        border-width: 8px;
        border-bottom-color: #FFEED4;
        right: 22px;
    }

    .shadow.opera-log-box:before {
        border-width: 10px;
        border-bottom-color: #E1B77E;
        right: 20px;
    }

    .shadow {
        display: none;
    }

    #tips:hover .shadow {
        display: block;
    }

    .mod-choose-input {
        position: relative;
    }

    .layui-layer-btn .layui-layer-btn0 {
        border-color: #c1c1c1;
        background-color: #fff;
        color: #000;
    }

    .layui-layer-iframe .layui-layer-btn,
    .layui-layer-page .layui-layer-btn {
        border-top: 1px solid #ececec;
        background-color: #fcfcfc;

    }

    .layui-layer-iframe .layui-layer-btn,
    .layui-layer-page .layui-layer-btn {
        padding: 5px;
    }

    .ui-label-green {
        background-color: #76C05D;
    }

    #manager .ui-label-warning.ui-label-green:hover {
        background-color: #7FC764;
    }
</style>


<div class="min-w inventory">
    <div class="bill-ser-top layui-form">
        <ul class="ul-inline cf">
            <li>
                <label>日期:</label>
                <input type="date" id="date" class="ui-input " value="" style="width: 125px;">
            </li>

            <!-- 选择仓库 -->
            {include file="admin@select/store"/}

            <!-- 选择商品类别 -->
            {include file="admin@select/goods_category"/}

            <li>
                <label>商品:</label>
                <span style="position:relative;" id="filter-goods">
                    <input type="text" id="goodsAuto_no" name="goods" class="ui-input w150">
                </span>
            </li>
            <li id="chkField" style="padding-top: 11px;height: 44px;box-sizing: border-box;">
                <input type="checkbox" name="box" id="showZero" lay-skin="primary" title="零库存" value="0">
                <input type="checkbox" name="box" id="isDelete" lay-skin="primary" title="显示禁用商品" value="1">
            </li>
            <li><a class="mrb ui-btn ui-btn-search" id="search">查询</a></li>
            <li id="tips" style="float: right; padding-right: 20px;position: relative;">
                <img src="/public/static/admin/css/img/pd-tips.png"
                    style="width: 20px; height: 20px; padding-left: 20px">
                <div id="tipsBox" class="shadow opera-log-box" style="position: absolute;right: 0;z-index: 5;top:35px;">
                    <p style="width: 200px">盘点完成前，请不要做出入库单据，盘点库存与查询时的系统库存比较计算盈亏。启用了序列号管理的商品，请通过"序列号盘点"处理</p>
                </div>
            </li>
        </ul>



        <!-- 下拉树选择（商品类别） -->
        {include file="admin@popup/goods_category"/}

    </div>

    <div class="wrapper btc">
        <div class="cf mod-search">
            <div class="ui-config-box">
                <span id="config" class="ui-config"><a href="#" class="ui-icon-config-new"></a>列设置</span>
            </div>
            <div class="fr">
                <a class="ui-btn-bill ui-btn-add ml10" id="savePD">保存盘点结果</a>
                <a class="ui-btn-bill ui-btn-add ml10" id="export">导出系统库存</a>
                <a class="ui-btn-bill ui-btn-add ml10" id="import">导入盘点库存</a>
                <a class="ui-btn-bill ui-btn-add ml10" id="save">生成盘点单据</a>
            </div>
        </div>

        <div class="grid-wrap">
            <table id="grid">
            </table>
            <div id="page"></div>
        </div>
        <div style="margin:10px 18px 0 0; " class="dn" id="handleDom">
        </div>
    </div>
    <!--  以隐藏的方式获取仓库的值 -->
    <div style="display:none;">
        <input type="text" id="id" value="{$id}" autocomplete="off" readonly="">
    </div>
</div>

<script type="text/javascript" src="/public/static/admin/easyui/jeasyui/jquery.easyui.min.js"></script>
<script>
    $(document).ready(function () {

        // 获取当前日期
        var myDate = new Date;
        var year = myDate.getFullYear(); //获取当前年
        var mon = myDate.getMonth() + 1; //获取当前月
        var date = myDate.getDate(); //获取当前日
        console.log(year + '-' + mon + '-' + date)
        if (mon < 10) {
            mon = "0" + mon;
        }
        if (date < 10) {
            date = "0" + date;
        }
        $("#date").val(year + '-' + mon + '-' + date);


    });

</script>

<script>
    var Pdata;
    var edit_row = 0;
    var edit_col = 0;
    var row_id = 0;
    var col_name = 0;
    $(document).ready(function () {
        // 点击查询按钮
        $("#search").click(function () {
            // 设置表格高度
            $("#grid").setGridHeight($(window).height() - 230 - $("#filter-menu").height() + "px");

            var startdate = $('#startdate').val();
            var goods_id = $('#goods_id').val();//选择商品
            var goods_category_id = $('#goods_category_id').val();//选择商品类别
            var store_id = $('#store_id').val();//选择仓库

            //此处可以添加对查询数据的合法验证 
            $("#grid").jqGrid('setGridParam', {
                datatype: 'json',
                postData: { 'ajax': '100', 'id': '{$id}', 'startdate': startdate, 'goods_id': goods_id, 'goods_category_id': goods_category_id, 'store_id': store_id }, //发送数据 
                page: 1
            }).trigger("reloadGrid"); //重新载入 
        });


        $("#grid").jqGrid({
            url: '/store/pandian/edit.html?ajax=100&id={$id}',
            datatype: "json",//数据来源，本地数据（local，json,jsonp,xml等）
            height: '90%',//高度，表格高度。可为数值、百分比或'auto'
            colNames: ["id", "仓库", "商品类别", "商品编号", "商品名称", "商品备注", "规格型号", "单位", "系统库存", "<span class='red'>*</span>盘点库存", "盘盈盘亏", "store_id", "goods_id", "unit_id"],
            colModel: [
                { name: 'id', hidden: true },
                { name: 'store_name', width: 100, label: "仓库" },
                { name: 'goods_category_name', width: 100, label: "商品类别" },
                { name: 'sn', width: 100, label: "商品编号" },
                { name: 'goods_name', width: 200, label: "商品名称" },
                { name: 'mark', width: 200, label: "商品备注" },
                { name: 'model', width: 100, label: "规格型号" },
                { name: 'unit_name', width: 50, label: "单位" },
                { name: 'stock', width: 100, align: 'right', label: "系统库存" },
                { name: 'pandian_stock', width: 100, align: 'right', editable: true, label: "盘点库存" },
                { name: 'difference', width: 100, align: 'right', label: "盘盈盘亏" },
                { name: 'store_id', hidden: true },
                { name: 'goods_id', hidden: true },
                { name: 'unit_id', hidden: true }
            ],
            cellEdit: true,
            cellurl: '/',
            sortable: true,  //排序

            rowNum: 500,
            rowList: [100, 200, 500],
            sortname: 'id',
            sortorder: "desc",
            pagerpos: "left",//分页位置

            pgbuttons: true,//翻页按钮
            rownumbers: true, //自增序号
            pagination: true,
            altRows: !0,
            gridview: !0,
            shrinkToFit: !1,
            cellLayout: 8,
            autowidth: !0,
            pager: "#page",
            viewrecords: !0,
            cmTemplate: {
                sortable: !1,
                title: !1
            },
            loadError: function (t, e, i) {
                parent.Public.tips({
                    type: 1,
                    content: "操作失败了哦，请检查您的网络链接！"
                })
            },

            beforeSaveCell: function (rowid, cellname, value, iRow, iCol) {
                edit_row = iRow;
                edit_col = iCol;
                row_id = rowid;

                var stock = parseFloat($("#grid").jqGrid("getCell", row_id, 'stock'));
                var difference = parseFloat(value / 1 - stock / 1).toFixed(2);
                if (difference > 0) {
                    $("#grid").jqGrid('setCell', row_id, edit_col+1 , '', {color : '#000'});
                }else{
                    $("#grid").jqGrid('setCell', row_id, edit_col+1 , '', {color : 'red'});
                }
                $("#grid").jqGrid("setCell", row_id, 'difference', difference);
            }
        });
    });
</script>


<script>

    $(".wrapper").on("click", "#savePD", function (e) {
        //先保存
        if (edit_row != 0 && edit_col != 0) {
            $("#grid").jqGrid("saveCell", edit_row, edit_col);
            edit_row = 0;
            edit_col = 0;
        }
        savedata();
    })

    function savedata() {
        var movedata = getJQAllData();
        var store_id = $('#store_id').val();
        var id = $('#id').val();
        store_id = store_id == '' ? 0 : store_id;

        $.ajax({
            type: 'POST',
            url: "save.html",
            data: { id: id, store_id: store_id, movedata: movedata },
            dataType: "json",
            async: false,
            error: function (request) {
                layer.msg("发送请求失败！");
            },
            success: function (data) {
                if (data.status == 1) {
                    layer.msg(data.msg);
                } else {
                    layer.msg(data.msg);
                }

            }
        });

    }
    function getJQAllData() {
        //拿到grid对象
        var obj = $("#grid");
        //获取grid表中所有的rowid值
        var rowIds = obj.getDataIDs();
        //初始化一个数组arrayData容器，用来存放rowData
        var arrayData = new Array();

        if (rowIds.length > 0) {
            for (var i = 0; i < rowIds.length; i++) {
                if (Object.values(obj.getRowData(rowIds[i]))[0] != '') {
                    arrayData.push(obj.getRowData(rowIds[i]));
                }
                //rowData=obj.getRowData(rowid);//这里rowid=rowIds[i];
                // console.log(obj.getRowData(rowIds[i].goodsname));
                // return false;
                // if (obj.getRowData(rowIds[i].goodsname != '')) {
                //   arrayData.push(obj.getRowData(rowIds[i]));
                // }
            }
        }

        return arrayData;
    }

    $(".wrapper").on("click", "#save", function (e) {
        //先保存
        if (edit_row != 0 && edit_col != 0) {
            $("#grid").jqGrid("saveCell", edit_row, edit_col);
            edit_row = 0;
            edit_col = 0;
        }
        savedata1();
    })

    function savedata1() {
        var movedata = getJQAllData();
        var store_id = $('#store_id').val();
        var id = $('#id').val();
        store_id = store_id == '' ? 0 : store_id;

        $.ajax({
            type: 'POST',
            url: "save.html",
            data: { id: id, store_id: store_id, movedata: movedata, pandian: 1 },
            dataType: "json",
            async: false,
            error: function (request) {
                layer.msg("发送请求失败！");
            },
            success: function (data) {
                if (data.status == 2) {
                    let content = '<div class="ui_content" style="height:100%;margin:auto;display: flex;align-items: center;"><div id="manager" style="margin: auto;"><ul>';
                    content += data.ck != 0 ? '<li><span class="ui-label ui-label-warning" onclick="pandian(' + data.pid + ', 0);" id="out">盘亏单</span></li>' : '';
                    content += data.rk != 0 ? '<li><span class="ui-label ui-label-warning ui-label-green" onclick="pandian(' + data.pid + ', 1);" id="in">盘盈单</span></li>' : '';
                    content += '</ul></div></div>';
                    layer.open({
                        type: 1,
                        title: "打开盘点单据确认后保存",
                        area: ['280px', '220px'], //宽高
                        content: content,
                        btn: ['取消']
                    });
                    layer.msg(data.msg, { time: 500, icon: 6 });
                } else {
                    layer.msg(data.msg);
                }

            }
        });

    }

    // info 盘点主id，
    // pandian 0盘亏，1盘盈
    function pandian(info, pandian) {
        pandian == 0 ? addPage('/store/invoiout/edit.html?pid=' + info, 'storage', '其他出库单') : addPage('/store/invoi/edit.html?pid=' + info, 'storage', '其他入库单');
    }
</script>


{include file="admin@public/footer" /}